package com.jiuwei.commons.mysql2word.utils;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.io.IOException;
import java.io.InputStream;

public class JsonUtil {
    private static ObjectMapper objectMapper = new ObjectMapper();

    public JsonUtil() {
    }

    public static ObjectMapper getObjectMapper() {
        return objectMapper;
    }

    public static <T> T json2Obj(InputStream json, Class<T> cls) {
        T pojo = null;

        try {
            pojo = objectMapper.readValue(json, cls);
        } catch (JsonParseException var4) {
            var4.printStackTrace();
        } catch (JsonMappingException var5) {
            var5.printStackTrace();
        } catch (IOException var6) {
            var6.printStackTrace();
        }

        return pojo;
    }

    public static <T> T json2Obj(String json, Class<T> cls) {
        T pojo = null;

        try {
            pojo = objectMapper.readValue(json, cls);
        } catch (JsonParseException var4) {
            var4.printStackTrace();
        } catch (JsonMappingException var5) {
            var5.printStackTrace();
        } catch (IOException var6) {
            var6.printStackTrace();
        }

        return pojo;
    }

    public static <T> T json2Obj(String json, Class<T> parametrized, Class... parameterClasses) {
        T pojo = null;
        JavaType javaType = objectMapper.getTypeFactory().constructParametrizedType(parametrized, parametrized, parameterClasses);

        try {
            pojo = objectMapper.readValue(json, javaType);
        } catch (JsonParseException var6) {
            var6.printStackTrace();
        } catch (JsonMappingException var7) {
            var7.printStackTrace();
        } catch (IOException var8) {
            var8.printStackTrace();
        }

        return pojo;
    }

    public static <T> T json2Obj(InputStream json, Class<T> parametrized, Class... parameterClasses) {
        T pojo = null;
        JavaType javaType = objectMapper.getTypeFactory().constructParametrizedType(parametrized, parametrized, parameterClasses);

        try {
            pojo = objectMapper.readValue(json, javaType);
        } catch (JsonParseException var6) {
            var6.printStackTrace();
        } catch (JsonMappingException var7) {
            var7.printStackTrace();
        } catch (IOException var8) {
            var8.printStackTrace();
        }

        return pojo;
    }

    public static String Obj2Json(Object pojo) {
        String json = null;

        try {
            json = objectMapper.writeValueAsString(pojo);
        } catch (JsonProcessingException var3) {
            var3.printStackTrace();
        }

        return json;
    }

    static {
        objectMapper.setSerializationInclusion(Include.NON_NULL);
        objectMapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);
        objectMapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        objectMapper.setDateFormat(DateUtil.DATE_TIME_FORMAT);
    }
}
